package org.itboys.mobile.controller.weixin.member;

import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.member.SundayWeixinMemberDto;
import org.itboys.mobile.service.mongo.weixin.member.SundayWeixinMemberService;
import org.itboys.mobile.util.HttpRequestUtil;
import org.itboys.mobile.util.MemberUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created on 2017/12/7.
 * @author 朱坤涛
 */
@Controller
@RequestMapping("/sunday/weixin/member")
public class SundayWeixinMemberController extends BaseController{

    @Autowired
    private SundayWeixinMemberService memberService;
    /**
     * 登录页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/doLogin")
    public String detail(
            HttpServletRequest request,
            Model model){
        return "/newcoo/weixin/doLogin";
    }

    /**
     * 登录
     * @param request
     * @param response
     * @param mobile
     * @param code
     */
    @RequestMapping(value="/login",method= RequestMethod.POST)
    public void login(HttpServletRequest request,
                        HttpServletResponse response,
                         @RequestParam(value = "mobile", required = true)String  mobile,
                        @RequestParam(value = "code", required = true)String  code
    ) {
        try{


            SundayWeixinMemberDto member= MemberUtil.getUser(request);
            memberService.login(member.getId(),mobile,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code1=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/coo/weixin/login----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }
    }


}
